home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap13 / Wanderer / FileView.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  6.0 KB  |  228 lines

  1. //***********************************************************************
  2. //
  3. //  FileView.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include <afxcview.h>
  9. #include "Resource.h"
  10. #include "FileView.h"
  11.  
  12. IMPLEMENT_DYNCREATE (CFileView, CListView)
  13.  
  14. BEGIN_MESSAGE_MAP (CFileView, CListView)
  15.     ON_WM_CREATE ()
  16.     ON_WM_DESTROY ()
  17.     ON_COMMAND_RANGE (ID_VIEW_LARGE_ICON, ID_VIEW_REPORT, OnChangeView)
  18.     ON_UPDATE_COMMAND_UI_RANGE (ID_VIEW_LARGE_ICON, ID_VIEW_REPORT,
  19.         OnUpdateViewUI)
  20.     ON_NOTIFY_REFLECT (LVN_GETDISPINFO, OnGetDispInfo)
  21.     ON_NOTIFY_REFLECT (LVN_COLUMNCLICK, OnColumnClick)
  22. END_MESSAGE_MAP ()
  23.  
  24. const DWORD CFileView::m_dwStyleList[4] = {
  25.     LVS_ICON,
  26.     LVS_SMALLICON,
  27.     LVS_LIST,
  28.     LVS_REPORT
  29. };
  30.  
  31. BOOL CFileView::PreCreateWindow (CREATESTRUCT& cs)
  32. {
  33.     if (!CListView::PreCreateWindow (cs))
  34.         return FALSE;
  35.  
  36.     cs.style &= ~LVS_TYPEMASK;
  37.     cs.style |= LVS_REPORT;
  38.     return TRUE;
  39. }
  40.  
  41. int CFileView::OnCreate (LPCREATESTRUCT lpcs)
  42. {
  43.     if (!CListView::OnCreate (lpcs) == -1)
  44.         return -1;
  45.  
  46.     m_imglLarge.Create (IDR_LARGEDOC, 32, 1, RGB (255, 0, 255));
  47.     m_imglSmall.Create (IDR_SMALLDOC, 16, 1, RGB (255, 0, 255));
  48.  
  49.     GetListCtrl ().SetImageList (&m_imglLarge, LVSIL_NORMAL);
  50.     GetListCtrl ().SetImageList (&m_imglSmall, LVSIL_SMALL);
  51.  
  52.     GetListCtrl ().InsertColumn (0, "File Name", LVCFMT_LEFT, 192);
  53.     GetListCtrl ().InsertColumn (1, "Size", LVCFMT_RIGHT, 96);
  54.     GetListCtrl ().InsertColumn (2, "Last Modified", LVCFMT_CENTER, 128);
  55.     return 0;
  56. }
  57.  
  58. void CFileView::OnDestroy ()
  59. {
  60.     FreeItemMemory ();
  61.     CListView::OnDestroy ();
  62. }
  63.  
  64. void CFileView::OnUpdate (CView* pView, LPARAM lHint, CObject* pHint)
  65. {
  66.     if (lHint != NULL) {
  67.         FreeItemMemory ();
  68.         GetListCtrl ().DeleteAllItems ();
  69.         InitList ((LPCTSTR) lHint);
  70.         return;
  71.     }
  72.     CListView::OnUpdate (pView, lHint, pHint);
  73. }
  74.  
  75. void CFileView::OnChangeView (UINT nID)
  76. {
  77.     ModifyStyle (LVS_TYPEMASK,
  78.         m_dwStyleList[nID - ID_VIEW_LARGE_ICON]);
  79. }
  80.  
  81. void CFileView::OnUpdateViewUI (CCmdUI* pCmdUI)
  82. {
  83.     DWORD dwCurrentStyle = GetStyle () & LVS_TYPEMASK;
  84.     pCmdUI->SetCheck (dwCurrentStyle ==
  85.         m_dwStyleList[pCmdUI->m_nID - ID_VIEW_LARGE_ICON]);
  86. }
  87.  
  88. void CFileView::OnGetDispInfo (NMHDR* pnmh, LRESULT* pResult)
  89. {
  90.     CString string;
  91.     LV_DISPINFO* plvdi = (LV_DISPINFO*) pnmh;
  92.  
  93.     if (plvdi->item.mask & LVIF_TEXT) {
  94.         ITEMINFO* pItem = (ITEMINFO*) plvdi->item.lParam;
  95.  
  96.         switch (plvdi->item.iSubItem) {
  97.  
  98.         case 0: // File name
  99.             ::lstrcpy (plvdi->item.pszText, (LPCTSTR) pItem->strFileName);
  100.             break;
  101.  
  102.         case 1: // File size
  103.             string.Format ("%u", pItem->nFileSizeLow);
  104.             ::lstrcpy (plvdi->item.pszText, (LPCTSTR) string);
  105.             break;
  106.  
  107.         case 2: // Date and time
  108.             CTime time (pItem->ftLastWriteTime);
  109.  
  110.             BOOL pm = FALSE;
  111.             int nHour = time.GetHour ();
  112.             if (nHour == 0)
  113.                 nHour = 12;
  114.             else if (nHour == 12)
  115.                 pm = TRUE;
  116.             else if (nHour > 12) {
  117.                 nHour -= 12;
  118.                 pm = TRUE;
  119.             }
  120.  
  121.             string.Format ("%d/%0.2d/%0.2d (%d:%0.2d%c)",
  122.                 time.GetMonth (), time.GetDay (), time.GetYear () % 100,
  123.                 nHour, time.GetMinute (), pm ? 'p' : 'a');
  124.             ::lstrcpy (plvdi->item.pszText, (LPCTSTR) string);
  125.             break;
  126.         }
  127.     }
  128. }
  129.  
  130. void CFileView::OnColumnClick (NMHDR* pnmh, LRESULT* pResult)
  131. {
  132.     NM_LISTVIEW* pnmlv = (NM_LISTVIEW*) pnmh;
  133.     GetListCtrl ().SortItems (CompareFunc, pnmlv->iSubItem);
  134. }
  135.  
  136. int CALLBACK CFileView::CompareFunc (LPARAM lParam1, LPARAM lParam2,
  137.     LPARAM lParamSort)
  138. {
  139.     ITEMINFO* pItem1 = (ITEMINFO*) lParam1;
  140.     ITEMINFO* pItem2 = (ITEMINFO*) lParam2;
  141.     int nResult;
  142.  
  143.     switch (lParamSort) {
  144.  
  145.     case 0: // File name
  146.         nResult = pItem1->strFileName.CompareNoCase (pItem2->strFileName);
  147.         break;
  148.  
  149.     case 1: // File size
  150.         nResult = pItem1->nFileSizeLow - pItem2->nFileSizeLow;
  151.         break;
  152.  
  153.     case 2: // Date and time
  154.         nResult = ::CompareFileTime (&pItem1->ftLastWriteTime,
  155.             &pItem2->ftLastWriteTime);
  156.         break;
  157.     }
  158.     return nResult;
  159. }
  160.  
  161. int CFileView::InitList (LPCTSTR pszPath)
  162. {
  163.     CString strPath = pszPath;
  164.     if (strPath.Right (1) != "\\")
  165.         strPath += "\\";
  166.     strPath += "*.*";
  167.  
  168.     HANDLE hFind;
  169.     WIN32_FIND_DATA fd;
  170.     int nCount = 0;
  171.  
  172.     if ((hFind = ::FindFirstFile ((LPCTSTR) strPath, &fd)) !=
  173.         INVALID_HANDLE_VALUE) {
  174.  
  175.         if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
  176.             AddItem (nCount, &fd);
  177.             nCount++;
  178.         }
  179.  
  180.         while (::FindNextFile (hFind, &fd)) {
  181.             if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  182.                 if (!AddItem (nCount, &fd))
  183.                     break;
  184.                 nCount++;
  185.         }
  186.         CloseHandle (hFind);
  187.     }
  188.     return nCount;
  189. }
  190.  
  191. BOOL CFileView::AddItem (int nIndex, WIN32_FIND_DATA* pfd)
  192. {
  193.     ITEMINFO* pItem;
  194.     try {
  195.         pItem = new ITEMINFO;
  196.     }
  197.     catch (CMemoryException* e) {
  198.         e->Delete ();
  199.         return FALSE;
  200.     }
  201.  
  202.     pItem->strFileName = pfd->cFileName;
  203.     pItem->nFileSizeLow = pfd->nFileSizeLow;
  204.     pItem->ftLastWriteTime = pfd->ftLastWriteTime;
  205.  
  206.     LV_ITEM lvi;
  207.     lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM; 
  208.     lvi.iItem = nIndex; 
  209.     lvi.iSubItem = 0; 
  210.     lvi.iImage = 0;
  211.     lvi.pszText = LPSTR_TEXTCALLBACK; 
  212.     lvi.lParam = (LPARAM) pItem;
  213.  
  214.     if (GetListCtrl ().InsertItem (&lvi) == -1)
  215.         return FALSE;
  216.  
  217.     return TRUE;
  218. }
  219.  
  220. void CFileView::FreeItemMemory ()
  221. {
  222.     int nCount = GetListCtrl ().GetItemCount ();
  223.     if (nCount) {
  224.         for (int i=0; i<nCount; i++)
  225.             delete (ITEMINFO*) GetListCtrl ().GetItemData (i);
  226.     }
  227. }
  228.